package Thread;

public class Demo10 {
    //线程的中断,Thread自带标志位
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("线程运行中...");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    //e.printStackTrace();

                    // [1] 立即退出
                    //break;
                    // [2] 稍后退出
                    System.out.println("新线程即将退出.");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                    break;
                    // [3] 啥都不做，即忽略异常
                }
            }
            System.out.println("新线程已经退出.");
        });

        t.start();
        Thread.sleep(5000);
        System.out.println("控制新线程退出.");
        t.interrupt();
    }
}
